package com.xyg.common.model;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;

/**
 * 分页工具
 * @author lzn@1805887
 *
 * @param <T>
 */
public class Pagination<T> {
	
	public Pagination(Pageable pageable,Page<T> impl) {
		Assert.notNull(pageable);
		this.pageable = pageable;
		Assert.notNull(impl);
		this.page = impl;
		this.endPage  = impl.getTotalPages()-1;
		this.current = impl.getNumber();
	}
	

	private final Page<T> page;
	
	private final Pageable pageable;
	
	/**
	 * @return the page
	 */
	public Page<T> getPage() {
		return page;
	}

	//末页码
	private final int endPage;
	//当前页码
	private final int current;

	public static final int DEFAULT_PAGE_COL_SIZE = 5;
	
	/**显示多少页*/
	private int pageColSize = DEFAULT_PAGE_COL_SIZE;
	

	/**
	 * @param pageColSize the pageColSize to set
	 */
	public void setPageColSize(int pageColSize) {
		this.pageColSize = pageColSize;
	}


	/**
	 * @return 分页组件页码
	 */
	protected int[] pageColList(){
		
		//设置显示开始页码
		int start = 0, 
			//设置显示结束页码
			end = endPage,half = pageColSize / 2;
		
		if (current - half >= 0 && current + half <= endPage){
			start = current - half;
			end = start + pageColSize - 1;
		} else if(current - half < 0){
			start = 0 ;
			end = pageColSize > endPage ? endPage : pageColSize - 1;
		} else if(current + half > endPage){
			end = endPage;
			start = end + 1 - pageColSize > 0 ? end + 1 - pageColSize : 0;
		}
		int [] cols = new int [end - start + 1];
		for (int i = 0 ; i < cols.length ; i++) {
			cols[i] = start + i;
		}
		return cols;
	}

	/**
	 * 分页组件
	 * @return
	 */
	public String pagination() {
		if (pageable instanceof PageRequestBean) {
			int first = 0;
			PageRequestBean bean = (PageRequestBean) pageable;
			if(page.getTotalPages() == 0)
				return "";
			StringBuilder result;
			result = new StringBuilder("<ul class=\"pagination\">");
			if (current == first){
				result.append("<li class=\"disabled\"><a href=\"#\">第一页</a></li>");
				result.append("<li class=\"disabled\"><a href=\"#\">上一页</a></li>");
			} else{
				result.append("<li><a href=\"#\" onclick=\"pagination('");
				result.append(bean.getFormName());
				result.append("','");
				result.append(bean.getTargetDivName());
				result.append("',");
				result.append(first);
				result.append(")\">第一页</a></li>");
				result.append("<li><a href=\"#\" onclick=\"pagination('");
				result.append(bean.getFormName());
				result.append("','");
				result.append(bean.getTargetDivName());
				result.append("',");
				result.append(bean.previous().getPageNumber());
				result.append(")\">上一页</a></li>");
			}
			
			for (int pageNo : pageColList()){
				if(current == pageNo) {
					result.append("<li class=\"active\"	><a href=\"#\">");
					result.append(pageNo + 1);
					result.append("</a></li>");
				}
				else {
					result.append("<li><a href=\"#\" onclick=\"pagination('");
					result.append(bean.getFormName());
					result.append("','");
					result.append(bean.getTargetDivName());
					result.append("',");
					result.append(pageNo);
					result.append(")\">");
					result.append(pageNo + 1);
					result.append("</a></li>");
				}
			}
			
			if (current == endPage){
				result.append("<li class=\"disabled\"><a href=\"#\">下一页</a></li>");
				result.append("<li class=\"disabled\"><a href=\"#\">最后一页</a></li>");
			} else {
				result.append("<li><a href=\"#\" onclick=\"pagination('");
				result.append(bean.getFormName());
				result.append("','");
				result.append(bean.getTargetDivName());
				result.append("',");
				result.append(bean.next().getPageNumber());
				result.append(")\">下一页</a></li>");
				result.append("<li><a href=\"#\" onclick=\"pagination('");
				result.append(bean.getFormName());
				result.append("','");
				result.append(bean.getTargetDivName());
				result.append("',");
				result.append(endPage);
				result.append(")\">最后一页</a></li>");
			}
			
			result.append("</ul>");
			return result.toString();
		}
		return "";
	}


}
